package org.jeecg.modules.oms.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.oms.entity.DesignFile;
import org.jeecg.modules.oms.entity.DesignFileAudit;
import org.jeecg.modules.oms.mapper.DesignFileAuditMapper;
import org.jeecg.modules.oms.mapper.DesignFileMapper;
import org.jeecg.modules.oms.service.IDesignFileAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 设计文件审核Service实现
 */
@Slf4j
@Service
public class DesignFileAuditServiceImpl extends ServiceImpl<DesignFileAuditMapper, DesignFileAudit> implements IDesignFileAuditService {
    
    @Autowired
    private DesignFileMapper designFileMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean submitAudit(DesignFileAudit fileAudit) {
        if (fileAudit == null) {
            throw new JeecgBootException("审核信息不能为空");
        }
        
        if (StringUtils.isBlank(fileAudit.getFileId())) {
            throw new JeecgBootException("文件ID不能为空");
        }
        
        // 验证文件是否存在
        DesignFile file = designFileMapper.selectById(fileAudit.getFileId());
        if (file == null) {
            throw new JeecgBootException("未找到对应文件");
        }
        
        // 设置订单ID
        if (StringUtils.isBlank(fileAudit.getOrderId())) {
            fileAudit.setOrderId(file.getOrderId());
        }
        
        // 设置初始状态
        fileAudit.setAuditStatus(0); // 0-待审核
        
        // 设置创建时间
        fileAudit.setCreateTime(new Date());
        
        return save(fileAudit);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean approveAudit(String auditId, String auditPersonId, String auditPersonName, String auditOpinion) {
        if (StringUtils.isBlank(auditId)) {
            throw new JeecgBootException("审核ID不能为空");
        }
        
        if (StringUtils.isBlank(auditPersonId)) {
            throw new JeecgBootException("审核人ID不能为空");
        }
        
        DesignFileAudit audit = getById(auditId);
        if (audit == null) {
            throw new JeecgBootException("未找到对应审核记录");
        }
        
        // 只有待审核状态可以审核通过
        if (audit.getAuditStatus() != 0) {
            throw new JeecgBootException("只有待审核状态可以审核通过");
        }
        
        audit.setAuditPersonId(auditPersonId);
        audit.setAuditPersonName(auditPersonName);
        audit.setAuditOpinion(auditOpinion);
        audit.setAuditStatus(1); // 1-审核通过
        audit.setAuditTime(new Date());
        
        return updateById(audit);
    }
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean rejectAudit(String auditId, String auditPersonId, String auditPersonName, String auditOpinion) {
        if (StringUtils.isBlank(auditId)) {
            throw new JeecgBootException("审核ID不能为空");
        }
        
        if (StringUtils.isBlank(auditPersonId)) {
            throw new JeecgBootException("审核人ID不能为空");
        }
        
        if (StringUtils.isBlank(auditOpinion)) {
            throw new JeecgBootException("拒绝时审核意见不能为空");
        }
        
        DesignFileAudit audit = getById(auditId);
        if (audit == null) {
            throw new JeecgBootException("未找到对应审核记录");
        }
        
        // 只有待审核状态可以审核拒绝
        if (audit.getAuditStatus() != 0) {
            throw new JeecgBootException("只有待审核状态可以审核拒绝");
        }
        
        audit.setAuditPersonId(auditPersonId);
        audit.setAuditPersonName(auditPersonName);
        audit.setAuditOpinion(auditOpinion);
        audit.setAuditStatus(2); // 2-审核拒绝
        audit.setAuditTime(new Date());
        
        return updateById(audit);
    }
    
    @Override
    public List<DesignFileAudit> getAuditsByFileId(String fileId) {
        if (StringUtils.isBlank(fileId)) {
            throw new JeecgBootException("文件ID不能为空");
        }
        
        return baseMapper.getAuditsByFileId(fileId);
    }
    
    @Override
    public List<DesignFileAudit> getAuditsByOrderId(String orderId) {
        if (StringUtils.isBlank(orderId)) {
            throw new JeecgBootException("订单ID不能为空");
        }
        
        return baseMapper.getAuditsByOrderId(orderId);
    }
    
    @Override
    public DesignFileAudit getLatestAuditByFileId(String fileId, Integer auditType) {
        if (StringUtils.isBlank(fileId)) {
            throw new JeecgBootException("文件ID不能为空");
        }
        
        if (auditType == null) {
            throw new JeecgBootException("审核类型不能为空");
        }
        
        return baseMapper.getLatestAuditByFileId(fileId, auditType);
    }
} 